package com.cxl.mapreduce._1wordCount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * 统计过程中对每一个MapTask的输出进行局部汇总，以减小网络传输量即采用Combiner功能
 * banzhang ni hao               <banzhang,4>
 * xihuan hadoop banzhang        < ni ,2>
 * banzhang ni hao				<hao,2>
 * xihuan hadoop banzhang		<xihuan,2>
 *     							<Hadoop,2>
 */
public class WordcountCombiner extends Reducer<Text, IntWritable, Text	, IntWritable>{
	
	IntWritable v = new IntWritable();

	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,
			Context context) throws IOException, InterruptedException {
		
		int sum = 0;
		// 1 累加求和
		for (IntWritable value : values) {
			sum += value.get();
		}
	
		v.set(sum);
		
		// 2 写出
		context.write(key, v);
	}
}
